package com.twopai.framelibrary.mvp;

import android.util.Log;

import androidx.lifecycle.Lifecycle;

import com.twopai.comm.progressdialog.KProgressHUD;
import com.twopai.mvpbase.BasePresenter;
import com.twopai.mvpbase.BaseView;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.AutoDisposeConverter;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

/**
 * @author twopai
 * Description：
 */
public abstract class BaseMvpActivity<T extends BasePresenter> extends BaseActivity implements BaseView {

    protected T mPresenter;
    private KProgressHUD progressBar;


    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroy();
        if(progressBar != null) {
            progressBar.dismiss();
        }
    }

    /**
     * 绑定生命周期 防止MVP内存泄漏
     *
     * @param <T>
     * @return
     */
    @Override
    public <T> AutoDisposeConverter<T> bindAutoDispose() {
        return AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
                .from(this, Lifecycle.Event.ON_DESTROY));
    }

    public KProgressHUD showProgress(String msg){
        Log.e("BaseHelper","showProgress");
        if (progressBar != null) {
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
            progressBar=null;
        }
        progressBar = KProgressHUD.create(this)
                .setDimAmount(0.5f)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true);
        if (msg != null&&!msg.isEmpty()) {
            progressBar.setLabel(msg);
        }
        progressBar.show();
        return progressBar;
    }

    public KProgressHUD showProgress(String msg,String etailsdMsg){
        Log.e("BaseHelper","showProgress");
        if (progressBar != null) {
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
            progressBar=null;
        }
        progressBar = KProgressHUD.create(this)
                .setDimAmount(0.5f)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true);
        if (msg != null&&!msg.isEmpty()) {
            progressBar.setLabel(msg);
        }
        if (etailsdMsg != null&&!etailsdMsg.isEmpty()) {
           progressBar.setDetailsLabel(etailsdMsg);
        }
        progressBar.show();
        return progressBar;
    }
    public void dismissProgress(){
        if (progressBar != null) {
            progressBar.dismiss();
            progressBar=null;
        }
        Log.e("BaseHelper","dismissProgress");
    }


//    /**
//     * 处理异常情况2
//     */
//    public void handleFailure(Throwable throwable) {
//        Log.e("onError","onErrorz="+throwable.getMessage());
//        String resultMsg = "网络错误";
//        if(throwable instanceof HttpException){
//            ResponseBody body = ((HttpException) throwable).response().errorBody();
//            try {
//                String s = body.string();
//                Log.e("onError", "eIndex=" + s);
//                if (s != null&&!s.isEmpty()) {
////                    OperGetTaskEntity operGetTaskEntity = new Gson().fromJson(s, OperGetTaskEntity.class);
////                    if (operGetTaskEntity != null) {
////                        AppToastManager.ToastLongTopCenter(this, operGetTaskEntity.getDetail());
////                    }
//                }
//            } catch (IOException e1) {
//                e1.printStackTrace();
//            }
//            Toast.makeText(this, resultMsg, Toast.LENGTH_SHORT).show();
//        }else if (throwable instanceof ConnectException) {
//            resultMsg = "网络连接失败";
//            Toast.makeText(this, resultMsg, Toast.LENGTH_SHORT).show();
////            AppToastManager.ToastLongTopCenter(this, resultMsg);
//        } else if (throwable instanceof NetworkErrorException) {
//            resultMsg = "网络错误";
//            Toast.makeText(this, resultMsg, Toast.LENGTH_SHORT).show();
////            AppToastManager.ToastLongTopCenter(this, resultMsg);
//        } else if (throwable instanceof TimeoutException || throwable instanceof SocketTimeoutException) {
//            resultMsg = "连接超时";
////            AppToastManager.ToastLongTopCenter(this, resultMsg);
//            Toast.makeText(this, resultMsg, Toast.LENGTH_SHORT).show();
//        }
//
//    }
}
